﻿using DearlerPlatform.Core.DataBaseCommon;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DearlerPlatform.Core.Repository
{
    public interface IRepository<TEntity> where TEntity : class
    {
        /// <summary>
        /// 删除一个实体
        /// </summary>
        /// <param name="id">ID主键</param>
        /// <returns>影响行数</returns>
        int Delete(long id);
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns>影响行数</returns>
        int Delete(long[] ids);
        /// <summary>
        /// 异步删除一个实体
        /// </summary>
        /// <param name="id">ID主键</param>
        /// <returns>影响行数</returns>
        Task<int> DeleteAsync(long id);
        /// <summary>
        /// 异步批量删除数据
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns>影响行数</returns>
        Task<int> DeleteAsync(long[] ids);
        /// <summary>
        /// 查询单个数据
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        TEntity Get(Expression<Func<TEntity, bool>> expression);
        /// <summary>
        /// 异步查询单个数据
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> expression);
        /// <summary>
        /// 返回所有数据集合
        /// </summary>
        /// <returns></returns>
        List<TEntity> GetList();
        /// <summary>
        /// 按条件返回数据集合
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        List<TEntity> GetList(Expression<Func<TEntity, bool>> expression);
        /// <summary>
        /// 异步返回所有数据集合
        /// </summary>
        /// <returns></returns>
        Task<List<TEntity>> GetListAsync();
        /// <summary>
        /// 异步按条件返回数据集合
        /// </summary>
        /// <param name="expression">条件</param>
        /// <returns></returns>
        Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> expression);
        
        /// 批量插入数据
        /// </summary>
        /// <param name="entity">数据集合</param>
        /// <returns>返回数据行数</returns>
        int Insert(List<TEntity> entity);
        /// <summary>
        /// 插入一个实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        /// <summary>
        TEntity Insert(TEntity entity);
        /// <summary>
        /// 异步批量插入数据
        /// </summary>
        /// <param name="entity">数据集合</param>
        /// <returns>返回数据行数</returns>
        Task<int> InsertAsync(List<TEntity> entity);
        /// <summary>
        /// 异步插入一个实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        Task<TEntity> InsertAsync(TEntity entity);
        /// <summary>
        /// 批量更新数据
        /// </summary>
        /// <param name="entity">实体集合</param>
        /// <returns>影响行数</returns>
        int Update(List<TEntity> entity);
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity">需要更新的实体</param>
        /// <returns>影响行数</returns>
        int Update(TEntity entity);
        /// <summary>
        /// 异步批量更新数据
        /// </summary>
        /// <param name="entity">实体集合</param>
        /// <returns>影响行数</returns>
        Task<int> UpdateAsync(List<TEntity> entity);
        /// <summary>
        /// 异步更新数据
        /// </summary>
        /// <param name="entity">需要更新的实体</param>
        /// <returns>影响行数</returns>
        Task<int> UpdateAsync(TEntity entity);
        /// <summary>
        /// 异步通用单表查询方法
        /// </summary>
        /// <param name="pars">查询参数</param>
        /// <returns>通用返回结果</returns>
        Task<CorePageData<List<TEntity>>> GetPageListAsync(QueryParameterBase pars);
        /// <summary>
        /// 仓储扩展方法:多表查询通用分页 
        /// 用法  CommonPage(db.Queryable<JoinTable1,JoinTable2>(...).Select(it=new class(){..}).MergeTable(),pars,orderbys,pageIndex,pagesize)
        /// </summary>
        /// <returns></returns>
        Task<object> CommonPage<ViewModel>(ISugarQueryable<ViewModel> query, QueryParameterBase pars);
    }
}
